*************************
* Set working directory *
*************************
cd ""

*********************
* Generate Figure 1 *
*********************
use covid.dta, clear
tsset date
twoway (tsline cases), tline(23nov2020 28dec2020) scheme(s1mono) ytitle(Covid-19 cases)


*********************
* Generate Figure 2 *
*********************
use main.dta, clear
tsset date
tsline polis if tin(1aug2020, 1apr2021), tline(24dec2020) tline(20nov2020) ytitle(Police calls) scheme(s1mono) saving(treatment, replace)
tsline polis if tin(1aug2019, 1apr2020), tline(24dec2019) tline(20nov2019) ytitle(Police calls) scheme(s1mono) saving(control, replace)
gr combine treatment.gph control.gph, col(1) ycommon

****************************
* Set up data for analysis *
****************************
* Create used time window
* Delete first period
drop if date<td(11,9,2019)
* Delete last period
drop if date>td(3,3,2021)
* Delete the period between the years (between 21,2,2020 to 21,9,2020)
drop if date>td(2,3,2020) & date<td(11,9,2020)

* Delete nov 20  to dec 23 (the period between treatments)
drop if date>td(19,11,2019) & date<td(24,12,2019)
drop if date>td(19,11,2020) & date<td(24,12,2020)

* Generate variables for DiD analysis
* Treated
gen treated=1 if date>td(2,3,2020)
replace treated=0 if treated==.
* Post
gen post=1 if date>=td(24,12,2020)
replace post=1 if date>=td(24,12,2019) & date<=td(21,2,2020)
replace post=0 if post==.
* Post*Treatment
gen interact=post*treated

bysort treated: gen dagar = _n
egen veckor = cut(dagar), at(1,8,15,22,29,36,43,50,57,64,71,78,85,92,99,106,113,120,127,134,141) icodes

replace covid_cases = covid_cases/1000

*************************************
* Regressions and tests for Table 1 *
*************************************
* Table 1, columns 1-3
reg polis treated post interact i.week i.dayofweek temp covid_cases, robust
outreg2 using table1, word replace bdec(3) cons keep(treated post interact temp covid_cases)
reg polisnatt treated post interact i.week i.dayofweek temp covid_cases, robust
outreg2 using table1, word append bdec(3) cons keep(treated post interact temp covid_cases)
reg polis treated post interact i.week i.dayofweek temp covid_cases if dayofweek==0| dayofweek == 5| dayofweek == 6, robust
outreg2 using table1, word append bdec(3) cons keep(treated post interact temp covid_cases)

* Tests for pre-trends
didregress (polis i.week i.dayofweek temp covid_cases) (interact), group(treated) time(veckor)
estat ptrends
didregress (polisnatt i.week i.dayofweek temp covid_cases) (interact), group(treated) time(veckor)
estat ptrends
didregress (polis i.week i.dayofweek temp covid_cases) (interact) if dayofweek==0| dayofweek == 5| dayofweek == 6, group(treated) time(veckor)
estat ptrends

* Drop weeks as to only use the smaller time window in Table 1, columns 4-6 
drop if veckor == 0
drop if veckor == 19
drop if veckor == 1
drop if veckor == 18
drop if veckor == 2
drop if veckor == 17
drop if veckor == 3
drop if veckor == 16
drop if veckor == 4
drop if veckor == 15

* Table 1, column2 4-6
reg polis treated post interact i.week i.dayofweek temp covid_cases, robust
outreg2 using table1, word append bdec(3) cons keep(treated post interact temp covid_cases)
reg polisnatt treated post interact i.week i.dayofweek temp covid_cases, robust
outreg2 using table1, word append bdec(3) cons keep(treated post interact temp covid_cases)
reg polis treated post interact i.week i.dayofweek temp covid_cases if dayofweek==0| dayofweek == 5| dayofweek == 6, robust
outreg2 using table1, word append bdec(3) cons keep(treated post interact temp covid_cases)

* Tests for pre-trends
didregress (polis i.week i.dayofweek temp covid_cases) (interact), group(treated) time(veckor)
estat ptrends
didregress (polisnatt i.week i.dayofweek temp covid_cases) (interact), group(treated) time(veckor)
estat ptrends
didregress (polis i.week i.dayofweek temp covid_cases) (interact) if dayofweek==0| dayofweek == 5| dayofweek == 6, group(treated) time(veckor)
estat ptrends